//
//  SFWelcomView.swift
//  SFWeiboPro
//
//  Created by happy on 2017/9/20.
//  Copyright © 2017年 happy. All rights reserved.
//

import UIKit
import SDWebImage

class SFWelcomView: UIView {

    @IBOutlet weak var iconView: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!
    
    @IBOutlet weak var iconViewBottomConstraint: NSLayoutConstraint!
    
    
    class func welcomView() -> SFWelcomView {
        
        let lib = UINib(nibName: "SFWelcomView", bundle: nil)
        
        let v = lib.instantiate(withOwner: nil, options: nil)[0] as! SFWelcomView
        
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        guard let iconString = SFNetworkManager.shareManager.userAccount.avatar_large,
               let iconUrl = URL(string: iconString) else {
            return
        }
        
        
        iconView.sd_setImage(with: iconUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        
        iconView.layer.cornerRadius = iconView.bounds.width * 0.5
        iconView.layer.masksToBounds = true
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        layoutIfNeeded()
        
        iconViewBottomConstraint.constant = bounds.size.height - 300
        
        UIView.animate(withDuration: 2.0,
                       delay: 0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: {
            self.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1.0, animations: { 
                
                self.tipLabel.alpha = 1.0
                
            }, completion: { (_) in
                self.removeFromSuperview()
            })
            
        }
    }
    
}
